# -*-coding:utf-8-*-
import sys
import requests
from lxml import etree
from YX_crawler.props import properties
import re
from YX_crawler.threads import yx_fail_queue, save_queue
from YX_crawler.crawler_func.crawler_base import crawler_base
from YX_crawler.DAO.yx_item import yx_item
from YX_crawler.DAO import proxies

props = properties.props
get_header = properties.get_header
get_proxies = proxies.get_proxy
reload(sys)
sys.setdefaultencoding('utf-8')


class yx_crawler(crawler_base):

    def get_infopage_by_itemid(self, itemID, proxy):
        url = props.get('yx_item_base') + itemID
        try:
            page_text = requests.get(url, headers=get_header(), proxies=proxy, timeout=5).content.decode("utf-8",
                                                                                                         'ignore')
            print proxy['http'] + 'status code' + page_text.status_code
            if page_text.status_code == 304:
                print 'itemID:' + itemID + "doesn't exists"
                return
            self.get_info_by_html(itemID, page_text)
        except requests.RequestException, req:
            print 'update_info.request:', req
            return
        except Exception, e:
            print 'update_info.exception:', e
        return

    def get_info_by_html(self, itemID, page_text):
        selector = etree.HTML(page_text)

        item = yx_item(itemID)
        item.item_title = selector.xpath('//*[@id="j-app"]/div/div[2]/div[1]/div[2]/div[1]/div[2]/span/text()')
        item.item_des = selector.xpath('//*[@id="j-app"]/div/div[2]/div[1]/div[2]/div[1]/div[3]/text()')
        item.item_good_rate = selector.xpath('//*[@id="j-app"]/div/div[2]/div[1]/div[2]/div[1]/div[2]/a/div/span/span[1]/text()')
        item.item_price = selector.xpath('//*[@id="j-app"]/div/div[2]/div[1]/div[2]/div[2]/div[1]/div/span[2]/span[2]/text()')
        item.item_img1 = selector.xpath('//*[@id="j-app"]/div/div[2]/div[1]/div[1]/div[2]/ul/li[1]/a/img/@src')
        item.item_img2 = selector.xpath('//*[@id="j-app"]/div/div[2]/div[1]/div[1]/div[2]/ul/li[2]/a/img/@src')
        item.item_img3 = selector.xpath('//*[@id="j-app"]/div/div[2]/div[1]/div[1]/div[2]/ul/li[3]/a/img/@src')
        item.item_img4 = selector.xpath('//*[@id="j-app"]/div/div[2]/div[1]/div[1]/div[2]/ul/li[4]/a/img/@src')
        item.item_img5 = selector.xpath('//*[@id="j-app"]/div/div[2]/div[1]/div[1]/div[2]/ul/li[5]/a/img/@src')

        item.item_comment1 = selector.xpath('//*[@id="j-app"]/div/div[2]/div[3]/div[1]/div[2]/div[1]/ul/li[1]/div[2]/div[3]/text()')
        item. item_comment2 = selector.xpath('//*[@id="j-app"]/div/div[2]/div[3]/div[1]/div[2]/div[1]/ul/li[2]/div[2]/div[3]/text()')
        item.item_comment3 = selector.xpath('//*[@id="j-app"]/div/div[2]/div[3]/div[1]/div[2]/div[1]/ul/li[3]/div[2]/div[3]/text()')
        item.item_comment4 = selector.xpath('//*[@id="j-app"]/div/div[2]/div[3]/div[1]/div[2]/div[1]/ul/li[4]/div[2]/div[3]/text()')
        item.item_comment5 = selector.xpath('//*[@id="j-app"]/div/div[2]/div[3]/div[1]/div[2]/div[1]/ul/li[5]/div[2]/div[3]/text()')

        self.bundle.append(item)
        return True

    def work(self, param=None, proxy=None):
        self.get_infopage_by_itemid(param, proxy)
        try:
            if len(self.bundle) > 0:
                save_queue.put(self.bundle)
            if not self.success:
                yx_fail_queue.put_nowait(param)
        except Exception, e:
            print 'queue put_error:', e
        return self.success